<template>
	<view>
		<!-- 搜索区域 -->
		<view class="search-box">
			<my-search
				@handleClick="goToSearch"
				:bgColor="'#C00000'" :radius="18"></my-search>
		</view>
		
		<!-- 轮播图区域 -->
		<swiper class="swiper" circular indicator-dots autoplay interval="1500" duration="500">
			<swiper-item v-for="(item,index) in swiperList" :key="item.goods_id">
				<navigator :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id" open-type="navigate" class="swiper-item">
					<image :src="item.image_src" mode="widthFix"></image>
				</navigator>
			</swiper-item>
		</swiper>
		
		<!-- 分类导航区域 -->
		<view class="nav-list">
			<navigator
				@click="handleNav(item)"
				v-for="(item,index) in navList" :key="index" class="nav-item">
				<image :src="item.image_src" class="nav-img"></image>
			</navigator>
		</view>
		
		<!-- 楼层区域 -->
		<view class="floor-list">
			<view
				v-for="(item,index) in floorList" :key="index"
				class="floor-item">
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				<view class="floor-img-box">
					<!-- 左侧大图容器盒 -->
					<navigator :url="item.product_list[0].url" class="left-img-box">
						<image
							:style="{ width: item.product_list[0].image_width + 'rpx' }"
							:src="item.product_list[0].image_src" mode="widthFix"></image>
					</navigator>
					<!-- 右侧小图容器盒 -->
					<view class="right-img-box">
						<navigator
							v-if="i !== 0"
							v-for="(it,i) in item.product_list" :key="i"
							:url="it.url"
							class="right-img-item">
							<image
								:style="{ width: it.image_width + 'rpx' }"
								:src="it.image_src" mode="widthFix"></image>
						</navigator>
					</view>
				</view>
				</view>
		</view>
	</view>
</template>

<script>
	import tabbarBadge from '@/mixins/tabbar-badge';
	export default {
		methods: {
			goToSearch(){											// 处理搜索功能
				console.log('搜索')
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			},
			handleNav(item){						// 处理导航的点击事件
				if(item.name === '分类'){
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			async getFloorList(){		// 获取楼层列表数据
				const { data: res } = await uni.$http.get('/api/public/v1/home/floordata')
				// 校验是否请求成功
				if (res.meta.status !== 200) {
					return uni.$showMsg()
				}
				// 处理数据
				res.message.forEach(floor => {
					floor.product_list.forEach(prod => {
						prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
					})
				})
				this.floorList = res.message
				console.log(this.floorList,'floorList')
				uni.$showMsg('数据请求成功')
			},
			async getNavList(){			// 获取导航列表数据
				const { data: res } = await uni.$http.get('/api/public/v1/home/catitems')
				// 校验是否请求成功
				if (res.meta.status !== 200) {
					return uni.$showMsg()
				}
				this.navList = res.message
				console.log(this.navList,'navList')
				uni.$showMsg('数据请求成功')
			},
			async getSwiperList() { // 获取轮播图列表数据
				const { data: res } = await uni.$http.get('/api/public/v1/home/swiperdata')
				// 校验是否请求成功
				if (res.meta.status !== 200) {
					return uni.$showMsg()
				}
				this.swiperList = res.message
				console.log(this.swiperList,'swiperList')
				uni.$showMsg('数据请求成功')
			}
		},
		mixins: [tabbarBadge],
		data() {
			return {
				floorList: [],	// 存储楼层列表数据
				navList: [],		// 存储导航列表数据
				swiperList: [], // 存储轮播图列表数据
			}
		},
		onLoad() {
			this.getSwiperList()
			this.getNavList()
			this.getFloorList()
		}
	}
</script>

<style lang="scss">
/* 搜索区域 */
.search-box {
	position: sticky;
	top: 0;
	/* 防止被覆盖 */
	z-index: 999;
}

/* 轮播图区域 */
.swiper {
	height: 330rpx;
	.swiper-item,image {
		width: 100%;
		height: 100%;
	}
}

/* 导航区域 */
.nav-list {
	display: flex;
	justify-content: space-around;
	margin: 15rpx 0;
	.nav-img {
		width: 128rpx;
		height: 128rpx;
	}
}

/* 楼层区域 */
.floor-list {
	.floor-item {
		padding: 0 20rpx;
		.floor-title {
			display: flex;
			width: 100%;
			height: 60rpx;
		}
		.floor-img-box {
			display: flex;
			.right-img-box {
				display: flex;
				flex-wrap: wrap;
			}
		}
	}
}
</style>